package com.iotek.bookstore.web.controlle;

import com.iotek.bookstore.modle.entity.Book;
import com.iotek.bookstore.modle.entity.Cart;
import com.iotek.bookstore.modle.service.serviceimpl.BookBizImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/login/cartServlet")
public class CartServlet extends CommonServlet {
    @Override
    public void doInsert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if(req.getSession().getAttribute("cart")==null){
            List<Cart>cartList=new ArrayList<Cart>();
            req.getSession().setAttribute("cart",cartList);
        }
        List<Cart>cartList=(List<Cart>) req.getSession().getAttribute("cart");

        int bookId=Integer.parseInt(req.getParameter("bookId"));
        String book=req.getParameter("book");
        String cover=req.getParameter("cover");
        double pricing=Double.valueOf(req.getParameter("pricing"));
        double price=Double.parseDouble(req.getParameter("price"));
        int number=Integer.parseInt(req.getParameter("number"));
        //保存购买书籍的库存信息
        int repertory=0;
        List<Book>books=new BookBizImpl().queryAll();
        for (Book book1:books){
           if ( book1.getBookId()==bookId){
               repertory=book1.getRepertory();
           }
        }

        for(Cart cart:cartList){
            if (cart.getBookId()==bookId){
                number=number+cart.getNumber();
                if(repertory>=number){
                    cart.setNumber(number);
                    resp.getWriter().write("成功添加到购物车!");
                    return;
                }else {
                    resp.getWriter().write("添加到购物车失败，库存不足！");
                    return;
                }

            }
        }
        Cart cart=new Cart(bookId,book,cover,pricing,price,number);
        if(repertory>=number){
            if(cartList.add(cart)){
                resp.getWriter().write("成功添加到购物车!");
            }
        }else {
            resp.getWriter().write("添加到购物车失败，库存不足！");
        }

        for(Cart cart1:cartList){
            System.out.println("图书ID:"+cart1.getBookId()+"--书名："+cart1.getBook()+
                "--定价："+cart1.getPricing()+"--当当价："+cart1.getPrice()+"--数量："+cart1.getNumber());
        }
    }

    @Override
    public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int bookId=Integer.parseInt(req.getParameter("bookId"));
        List<Cart>cartList=(List<Cart>) req.getSession().getAttribute("cart");
        if(bookId==-1){
                    cartList.clear();
            return;
        }
        for(int i=0;i<cartList.size();i++){
            if(cartList.get(i).getBookId()==bookId){
                cartList.remove(i);
            }
        }
        //保存
        req.getSession().setAttribute("cart",cartList);
    }

    @Override
    public void doUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int bookId=Integer.parseInt(req.getParameter("bookId"));
        int number=Integer.parseInt(req.getParameter("number"));
        //保存购买书籍的库存信息
        int repertory=0;
        List<Book>books=new BookBizImpl().queryAll();
        for (Book book1:books){
            if ( book1.getBookId()==bookId){
                repertory=book1.getRepertory();
            }
        }
        if(repertory<number){
            resp.getWriter().write("变更失败，库存不足!");
            return;
        }
        List<Cart>cartList=(List<Cart>) req.getSession().getAttribute("cart");
        for(int i=0;i<cartList.size();i++){
            if(cartList.get(i).getBookId()==bookId){
                cartList.get(i).setNumber(number);
            }
        }
        //保存
        req.getSession().setAttribute("cart",cartList);
    }

    @Override
    public void doQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Cart>cartList=(List<Cart>) req.getSession().getAttribute("cart");
        if(cartList!=null&&cartList.size()>0){
            req.setAttribute("flag",1);
        }else {
            req.setAttribute("flag",2);
        }
        if(cartList!=null){
            for(Cart cart1:cartList){
                System.out.println("图书ID:"+cart1.getBookId()+"--书名："+cart1.getBook()+
                    "--定价："+cart1.getPricing()+"--当当价："+cart1.getPrice()+"--数量："+cart1.getNumber());
            }
        }
        req.setAttribute("cart",cartList);
        if(req.getParameter("order")==null){
            req.getRequestDispatcher("cart.jsp").forward(req,resp);
        }else if(req.getParameter("order").equals("order")){
            req.getRequestDispatcher("order.jsp").forward(req,resp);
        }


    }
}
